﻿Imports Framework.Data

Public Class SharedItemsFiles
    Shared Sub New()
        Framework.Security.KeyMaster.RegisterDissolver(Of Entities.SharedItemFile)(AddressOf SecurityGroupDissolver)
    End Sub
    Private Shared Function SecurityGroupDissolver(ByVal file As Entities.SharedItemFile) As Generic.HashSet(Of Integer)
        Dim clsItem As Entities.SharedItem = SharedItems.GetById(file.SharedItemId)
        Dim clsShare As Entities.Share = Shares.GetById(clsItem.ShareId)
        Return clsShare.SecurityGroupIds
    End Function

    Public Shared Function GetAllBySharedItemId(ByVal sharedItemId As Integer) As Generic.List(Of Entities.SharedItemFile)
        Return DataStore.SharedItemsFilesEx.GetAllBySharedItemId(sharedItemId)
    End Function
    Public Shared Function GetAllBySharedItemId(ByVal sharedItemId As Integer, ByVal transactionScope As TransactionScope) As Generic.List(Of Entities.SharedItemFile)
        Return DataStore.SharedItemsFilesEx.GetAllBySharedItemId(sharedItemId)
    End Function
    Public Shared Function GetBySHA512HashAndSizeNotOrphaned(ByVal sha512Hash As String, ByVal size As Long) As Entities.SharedItemFile
        Return DataStore.SharedItemsFilesEx.GetBySHA512HashAndSizeNotOrphaned(sha512Hash, size)
    End Function
    Public Shared Function GetAllBySize(ByVal size As Long) As Generic.List(Of Entities.SharedItemFile)
        Return DataStore.SharedItemsFilesEx.GetAllBySize(size)
    End Function

    Public Shared Sub ReevalOrphanedSince(ByVal transactionScope As TransactionScope)
        DataStore.SharedItemsFilesEx.ReevalOrphanedSince(transactionScope)
    End Sub
    Public Shared Sub RemoveOldOrphans()
        Dim dtSince As Date = Now.Subtract(TimeSpan.FromDays(180))
        Using t As New TransactionScope(TransactionScope.TransactionMode.NullTransaction)
            RemoveOldOrphans(dtSince, t)
        End Using
    End Sub
    Public Shared Sub RemoveOldOrphans(ByVal orphanedSince As Date, ByVal transactionScope As TransactionScope)
        DataStore.SharedItemsFilesEx.RemoveOldOrphans(orphanedSince, transactionScope)
    End Sub

    Public Shared Sub Save(ByVal file As Entities.SharedItemFile, ByVal transactionScope As TransactionScope)
        DataStore.SharedItemsFilesEx.Save(file, transactionScope)
    End Sub

    Public Shared Sub DeleteBySharedItemId(ByVal sharedItemId As Integer, ByVal transactionScope As TransactionScope)
        Dim aFiles As Generic.List(Of Entities.SharedItemFile)
        aFiles = DataStore.SharedItemsFilesEx.GetAllBySharedItemId(sharedItemId, transactionScope)
        For Each clsFile As Entities.SharedItemFile In aFiles
            Delete(clsFile.Id, transactionScope)
        Next
    End Sub
    Public Shared Sub Delete(ByVal id As Integer, ByVal transactionScope As TransactionScope)
        DataStore.SharedItemsFilesEx.SetFileAsOrphan(id, transactionScope)
    End Sub
    Public Shared Function FindOrphan(ByVal fileSize As Long, ByVal sha512Hash As String) As Entities.SharedItemFile
        Dim clsFile As Entities.SharedItemFile = DataStore.SharedItemsFilesEx.GetBySHA512HashAndSize(sha512Hash, fileSize)
        If clsFile IsNot Nothing AndAlso clsFile.SharedItemId = 0 Then
            Return clsFile
        Else
            Return Nothing
        End If
    End Function
    Public Shared Function FindOrphan(ByVal fileSize As Long, ByVal sha512FirstMb As String, ByVal sha512LastMb As String) As Entities.SharedItemFile
        For Each clsFile As Entities.SharedItemFile In DataStore.SharedItemsFilesEx.GetAllBySize(fileSize)
            If clsFile.SharedItemId = 0 AndAlso clsFile.SHA512HashForFirstMb = sha512FirstMb AndAlso clsFile.SHA512HashForLastMb = sha512LastMb Then
                Return clsFile
            End If
        Next
        Return Nothing
    End Function
End Class
